/*
 *  Copyright 2010 Wesley Gooch, Ryan Nix, Cody Wenzel.
 *  Montana State University - Spring Semester 2010
 *  Computer Science 450 - Compilers
 *  Project: Micro-Pascal Compiler
 */
package msu.compiler.scanner;

import java.util.HashMap;

/**
 *
 * @author Wesley
 */
public class TokenLookup {

    private HashMap<String, Byte> reserved;
    private HashMap<String, Byte> identifier;
    private String[] reserved_s = {"and", "begin", "div", "do", "downto",
        "else", "end", "fixed", "float", "for", "function", "if", "integer",
        "mod", "not", "or", "procedure", "program", "read", "repeat", "then",
        "to", "until", "var", "while", "write"};
    private byte[] reserved_b = {Token.MP_AND, Token.MP_BEGIN, Token.MP_DIV,
        Token.MP_DO, Token.MP_DOWNTO, Token.MP_ELSE, Token.MP_END,
        Token.MP_FIXED, Token.MP_FLOAT, Token.MP_FOR, Token.MP_FUNCTION,
        Token.MP_IF, Token.MP_INTEGER, Token.MP_MOD, Token.MP_NOT, Token.MP_OR,
        Token.MP_PROCEDURE, Token.MP_PROGRAM, Token.MP_READ, Token.MP_REPEAT,
        Token.MP_THEN, Token.MP_TO, Token.MP_UNTIL, Token.MP_VAR,
        Token.MP_WHILE, Token.MP_WRITE};

    public TokenLookup() {
        reserved = new HashMap<String, Byte>();
        //puts all the values into the hashmap
        for (int i = 0; i < 26; i++) {
            reserved.put(reserved_s[i], reserved_b[i]);
        }
    }

    /**
     * Returns the token type if the lexeme is found in the lookup table.
     * If nothing is found then a -1 is returned to indicate this.
     * 
     * @param lexeme the lexeme to compare againts the lookup tables.
     * @return the token type or -1 if failed to resolve.
     */
    public byte resolve(String lexeme) {
        byte type = -1;
        //compare with local hashmap of reserved lexemes
        if (reserved.containsKey(lexeme.toLowerCase()) == true) {
            return reserved.get(lexeme.toLowerCase());
        } else {
            return type;
        }
    }
}
